﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TransparentForm
{
    public partial class Form1 : Form
    {
        private readonly Bitmap _backgroundBitmap;
        public Form1()
        {
            InitializeComponent();
            CreateRegion(this, TransparentForm.Properties.Resources.游戏手柄);
            this.MouseMove += Form1_MouseMove;
            this.MouseDown += Form1_MouseDown;
            this.MouseUp += Form1_MouseUp;
            // 启用双缓冲防止闪烁
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
        }

        // 实现窗体拖动（因为没有标题栏了）
        private bool isDragging = false;
        private Point lastCursor;
        private Point lastForm;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = true;
                lastCursor = Cursor.Position;
                lastForm = this.Location;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                this.Location = new Point(
                    lastForm.X + Cursor.Position.X - lastCursor.X,
                    lastForm.Y + Cursor.Position.Y - lastCursor.Y
                );
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDragging = false;
        }
        /// <summary>
        /// 创建支持位图区域的控件（Form 或 Button），基于透明度自动裁剪区域
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="bitmap">位图（建议使用 PNG 格式，包含 Alpha 通道）</param>
        public static void CreateRegion(Control control, Bitmap bitmap)
        {
            if (control == null || bitmap == null) return;
            // 设置控件大小
            control.Width = bitmap.Width;
            control.Height = bitmap.Height + 35;

            if (control is Form form)
            {
                // 设置窗体样式
                form.FormBorderStyle = FormBorderStyle.None;
                form.StartPosition = FormStartPosition.Manual; // 避免系统自动居中
                form.ShowInTaskbar = true;
                form.TransparencyKey = Color.Magenta; // 用于隐藏背景色

                form.BackgroundImage = bitmap;

                var graphicsPath = CalculateGraphics(bitmap);
                form.Region = new Region(graphicsPath);
            }
            else if (control is Button button)
            {
                button.Text = "";
                button.Cursor = Cursors.Hand;
                button.FlatStyle = FlatStyle.Flat;
                button.FlatAppearance.BorderSize = 0;
                button.BackgroundImageLayout = ImageLayout.Stretch;
                button.BackgroundImage = bitmap;

                var graphicsPath = CalculateGraphics(bitmap);
                button.Region = new Region(graphicsPath);
            }
        }

        private static GraphicsPath CalculateGraphics(Bitmap bitmap)
        {
            //创建 GraphicsPath
            GraphicsPath graphicsPath = new GraphicsPath();
            //第一个找到点的X
            int colPixel = 0;
            // 偏历所有行（Y方向）
            for (int row = 0; row < bitmap.Height; row++)
            {
                //重设
                colPixel = 0;
                //偏历所有列（X方向）
                for (int col = 0; col < bitmap.Width; col++)
                {
                    //如果是不需要透明处理的点则标记，然后继续偏历
                    if (bitmap.GetPixel(col, row).A == 255)
                    {
                        //记录当前
                        colPixel = col;
                        //建立新变量来记录当前点
                        int colNext = col;
                        ///从找到的不透明点开始，继续寻找不透明点,一直到找到或则达到图片宽度
                        for (colNext = colPixel; colNext < bitmap.Width; colNext++)
                            if (bitmap.GetPixel(colNext, row).A < 255)
                                break;
                        //将不透明点加到graphics path
                        graphicsPath.AddRectangle(new Rectangle(colPixel, row, colNext - colPixel, 1));
                        col = colNext;
                    }
                }
            }
            return graphicsPath;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("你点击了按钮2");
        }

        private void label2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("你点击了按钮1");
        }
    }
}
